#include "fileutils.h"

#include "core.h"
#include "logs/log.h"

// -------------------------------- FILE 
long flength(FILE *fp) {
    fseek(fp, 0, SEEK_SET);
    fseek(fp, 0, SEEK_END);
    long index = ftell(fp);
    return index;
}

int frowcount(FILE *fp) {
    fseek(fp, 0, SEEK_SET);
    char enter = '\n';
    bool eof = false;
    int lines = 0;
    while (!eof) {
        char c = fgetc(fp);
        if (c == -1) {
            eof = true;
            continue;
        }
        if (c == '\n') {
            lines++;
        }
    }
    return lines;
}

int freadline(FILE *fp, char *buffer){
    char enter = '\n';
    int c, index;
    c = index = 0;
    if (feof(fp)) return EOF;
    while ((c = fgetc(fp)) != EOF && (c != enter)) {
        buffer[index++] = c;
    }
    return !EOF;
}

// can't use of /proc file path
size_t freadall(FILE *fp, char *buffer) {
    long size = flength(fp);
    fseek(fp, 0, SEEK_SET);
    // printf("%s: %ld\n", __FUNCTION__, size);
    return fread(buffer, 1, size, fp);
}


// -------------------------------- Wrap
long FileLength(const char* path) {
    long len = 0;
    errno = 0;
    FILE *fp = fopen(path, "r");
    // printf("errno: %d\n", errno);
    if (errno != 0) {
        ERROR_LOG("errno值： %d",errno);
        ERROR_LOG("错误信息： %s",strerror(errno));
        return -1;
    }
    fseek(fp, 0, SEEK_SET);
    len = flength(fp);
    fclose(fp);
    return len;
}

int FileRowCount(const char* path) {
    int line = 0;
    FILE *fp = fopen(path, "r");
    fseek(fp, 0, SEEK_SET);
    line = frowcount(fp);
    fclose(fp);
    return line;
}

// -------------------------------- Custom
bool FileExists(const char *path) {
    errno = 0;
    FILE *fp = NULL;

    if((fp = fopen(path, "r")) == NULL) {
        ERROR_LOG("%s [%s]",strerror(errno), path);
        return false;
    }
    
    if (ferror(fp) != 0) {
        ERROR_LOG("错误信息： %s",strerror(errno));
        return false;
    }
    fclose(fp);
    return true;;
}

bool FileCreateEmpty(const char *path) {
    if (FileExists(path)) return true;

    errno = 0;
    FILE *fp = NULL;

    if ((fp = fopen(path, "w")) == NULL) {
        ERROR_LOG("%s [%s]",strerror(errno), path);
        return false;
    }

    fclose(fp);
    return true;;
}

// -- 
long FileReadToBuffer(const char *path, char *buffer, size_t len) {
    if (!FileExists(path)) return -1;

    FILE *fp = NULL;
    long length = -1;

    errno = 0;
    if ((fp = fopen(path, "r")) == NULL) {
        ERROR_LOG("错误信息: %s", strerror(errno));
    }

    length = fread(buffer, 1, len, fp); 
    
    fclose(fp);
    return length;
}


long FileWirteWithBuffer(const char *path, char *buffer, size_t len) {
    if (!FileExists(path)) return -1;

    FILE *fp = NULL;
    long length = -1;

    errno = 0;
    if ((fp = fopen(path, "w")) == NULL) {
        ERROR_LOG("错误信息: %s", strerror(errno));
    }
    // length = fwrite(const void *restrict __ptr, size_t __size, size_t __nitems, FILE *restrict __stream)
    length = fwrite(buffer, 1, len, fp); 
    
    fclose(fp);
    return length;
}